package com.yiwyn.flowdemo.controller;

import com.yiwyn.flowdemo.domain.ProcessInstance;
import com.yiwyn.flowdemo.flow.common.FlowDomainEnum;
import com.yiwyn.flowdemo.flow.engine.FlowEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@RestController
@RequestMapping("/approval")
public class ApproveController {

    @Autowired
    private FlowEngine flowEngine;


    @GetMapping("/apply")
    public void apply(String applyUserId) throws Exception {
        ProcessInstance instance = flowEngine.createInstance(FlowDomainEnum.LEAVE.getDomain(), applyUserId, null, true);
        log.info("发起流程成功:{}", instance);
    }


    @GetMapping("/claim")
    public void approve(String instanceId, String approveUserId) throws Exception {
        flowEngine.claimTask(instanceId, approveUserId);
    }

    @GetMapping("/complete")
    public void complete(String instanceId, String conclusion, Integer day) throws Exception {
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("conclusion", conclusion);
        variables.put("day", day);
        flowEngine.complete(instanceId, variables);
    }

}
